require 'rake/clean'

PROJECT = "test"
EXE_FILE = "#{PROJECT}.exe"

OBJ_PATH = "obj"
OBJ_EXT = ".o"

INC_PATHS = %w(../src/sexp ../src/mlisp)
INCLUDES = INC_PATHS.map {|x| "-I#{x}"}.join(" ")

C_OPT = "#{INCLUDES}"
CC = "gcc #{C_OPT} -c"

CPP_OPT = "#{INCLUDES}"
CPP = "g++ #{CPP_OPT} -c"

LIBS = "-lgtest_main -lgtest"

LINK_OPT = ""
LINK = "g++ #{LINK_OPT}"

####

srcs  = FileList["*.cpp"]
srcs += FileList["../src/sexp/*.c"]
srcs += FileList["../src/mlisp/*.c"]
objs = srcs.ext(OBJ_EXT).collect {|item| "#{OBJ_PATH}/#{File.basename(item)}"}

# find source from target file name
def find_source(srcs)
	lambda do |target_name|
		base = File.basename target_name, ".*"
		srcs.find {|s| File.basename(s, ".*") == base}
	end
end

# rule: obj for src
rule /.#{OBJ_EXT}$/ => [find_source(srcs), OBJ_PATH] do |t|
	sh "#{CC} -o#{t.name} #{t.source}"
end

CLEAN.include(objs, OBJ_PATH, EXE_FILE)

desc "Build exe file (default)"
task :default => EXE_FILE

task "all" => [:default, "check"] do
end

file EXE_FILE => objs do |t|
	sh "#{LINK} -o#{t.name} #{t.prerequisites.join(' ')} #{LIBS}"
end

directory OBJ_PATH


task "check" do
	sh "./#{EXE_FILE}"
end
